package user

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"
	"go-weixin/common/ctxdata"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserUpdateLogic) UserUpdate(req *types.UserUpdateReq) (resp *types.UserUpdateResp, err error) {
	uid := ctxdata.GetUidFromCtx(l.ctx)
	ctx, cannFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer cannFunc()
	_, err = l.svcCtx.UserRpc.UserUpdate(ctx, &usercenter.UserUpdateReq{
		Id:       uid,
		Name:     req.Name,
		Phone:    req.Phone,
		OpenId:   req.OpenId,
		Email:    req.Email,
		Country:  req.Country,
		Gender:   req.Gender,
		Activity: req.Activity,
	})
	return
}
